﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using POS.Framework;
using DevExpress.Utils;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using Model.Bill;
using BLL;
using DevExpress.Xpo.DB;
using BLL.Enums;

namespace POS.ModForm.Bill
{
    public partial class frmPaymentDebt : DevExpress.XtraEditors.XtraForm
    {
        MyGridChedkMarksSelectionHelper ChedkSelect { get; set; }
        public frmPaymentDebt()
        {
            InitializeComponent();
        }

        private void frmPaymentDebt_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                this.gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                this.ChedkSelect = new MyGridChedkMarksSelectionHelper(this.gridView);
                this.repositoryItemLookUpEditOrderType.DataSource = EnumManage.GetDictionaryEntry(typeof(OrderType));
                SetButtonClickEvent();
                SearchData();
            }
        }
        void SetButtonClickEvent()
        {
            this.listButtonUC.IsHide(new List<string> { "btnNew"});
            this.listButtonUC.Query.Click+=Query_Click;
            this.listButtonUC.PayDebt.Click+=PayDebt_Click;
        }
        void Query_Click(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                SearchData();
            }
        }
        void SearchData()
        {
            GroupOperator goFilter = new GroupOperator();
            goFilter.Operands.Add(CriteriaOperator.Parse("StatusID<>?", (int)PaymentStatus.Complete));
            if (!string.IsNullOrEmpty(this._SellDateStart.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("OrderID.SellDate>=?", this._SellDateStart.Text.Trim()));
            if (!string.IsNullOrEmpty(this._SellDateEnd.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("OrderID.SellDate<?", this._SellDateEnd.Text.Trim() + " 23:58:58"));
            if (!string.IsNullOrEmpty(this._PayDateStart.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("PayDate>=?", this._PayDateStart.Text.Trim()));
            if (!string.IsNullOrEmpty(this._PayDateEnd.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse("PayDate<?", this._PayDateEnd.Text.Trim() + " 23:58:58"));
            if (!string.IsNullOrEmpty(this._ProviderName.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse(string.Format("OrderID.ProviderID.ProviderName like '{0}'", this._ProviderName.Text.Trim())));
            XPCollection<TB_OrderPay> sd = new DevExpress.Xpo.XPCollection<TB_OrderPay>(DatabaseSchema.Session, goFilter, new SortProperty("CreateDT", SortingDirection.Descending));
            this.gridControl.DataSource = sd;
            SpecialGridView.SetGridViewIndicatorWidth(sd.Count, this.gridView);
        }
        void PayDebt_Click(object sender, EventArgs e)
        {
            if (this.ChedkSelect.SelectedCount <= 0)
            {
                MsgBox.ShowWarning("请选中要付款的销售订单!");
                return;
            }
            try
            {
                string providerID = string.Empty;
                string providerName = string.Empty;
                decimal amount=0;
                List<string> listBill = new List<string>();
                foreach (TB_OrderPay od in this.ChedkSelect._Selection)
                {
                    listBill.Add(od.OrderID.OrderID);
                    amount += od.AmountCope;
                    if (string.IsNullOrEmpty(providerID))
                    {
                        providerID = od.OrderID.ProviderID.ProviderID;
                        providerName = od.OrderID.ProviderID.ProviderName;
                    }
                    else
                    {
                        if (providerID != od.OrderID.ProviderID.ProviderID)
                        {
                            MsgBox.ShowWarning("不能选择多个付款业务伙伴进行付款!");
                            return;
                        }
                    }
                }
                using (frmProviderDebtDialog fpm = new frmProviderDebtDialog(providerID, providerName,listBill, amount))
                {
                    if (fpm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        MsgBox.ShowTips("客户支付欠款成功！");
                        SearchData();
                    }
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowWarning(ex.Message);
                return;
            }
        }
    }
}